
package com.zsrt.jmj.utils;


import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
import java.net.URL;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;

import com.zsrt.jmj.common.constant.Constants;
import com.zsrt.jmj.common.entity.DBEntity;




/**
 * 跟数据库相关的工具
 * @author jmj
 * @date 2017年5月24日
 * @time 下午5:27:45
 */
public class DBUtil {
	
	
	/**
	 * 关闭资源
	 * @param ps
	 * @param connection
	 */
	public static void closeResources(PreparedStatement ps,Connection connection){
		if(ps !=null){
			try {
				ps.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		if(null != connection){
			try {
				connection.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
			
		}
	}
	
	public static void closeResources(ResultSet rs,PreparedStatement ps,Connection connection){
		if(null != rs){
			try {
				rs.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		if(ps !=null){
			try {
				ps.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		if(null != connection){
			try {
				connection.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
			
		}
	}
	/**
	 * 获得对应数据库的连接
	 * @return
	 */
	public static Connection getConnect(){
		
		Properties properties= new Properties();
		URL res= Thread.currentThread().getContextClassLoader().getResource("");
		String path = res.getPath()+Constants.FILENAME;
		System.out.println(path);
		
		
		DBEntity init = DBUtil.init(path);
		String url = init.getUrl();
		String user = init.getUser();
		String password = init.getPassword();
		Connection connection =null;
		try {
			Class.forName(init.getDriver());
			
			connection = DriverManager.getConnection(url, user, password);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return connection;
	}
	
	/**
	 * 读取指定的配置文件 返回连接数据库的4个关键因素
	 * @param location
	 * @return
	 */
	public static DBEntity init(String location){
		
		DBEntity de = new DBEntity();
		File file = new File(location);
		BufferedReader br=null;
		Reader reader =null;
		try {
			reader = new FileReader(file);
			
			br = new BufferedReader(reader);
			
			String jieguo ="";
			
			while((jieguo = br.readLine()) !=null){
				if(jieguo !=null && jieguo.contains("=")){
					String[] dbs = jieguo.split("=");
					String key   = dbs[0];
					String value = dbs[1];
					if(Constants.DRIVER.equals(key)){
						de.setDriver(value);;
					}else if(Constants.URL.equals(key)){
						de.setUrl(value);
					}else if(Constants.USER.equals(key)){
						de.setUser(value);
					}else{
						de.setPassword(value);
					}
				}
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally {
			if(br !=null){
				try {
					br.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if(reader != null){
				try {
					reader.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return de;
	}
	
	
	/**
     * 获取项目的相对路径下文件的绝对路径
     * @param parentDir
     * 目标文件的父目录 , 例如说 , 工程的目录下 , 有 lib 与 bin 和 conf 目录 , 那么程序运行于 lib or
     * bin, 那么需要的配置文件却是 conf 里面 , 则需要找到该配置文件的绝对路径
     * @param fileName
     * 文件名
     * @return 一个绝对路径
     *//*
    public static String getPath(String parentDir, String fileName) {

        String path = null;

        String userdir = System.getProperty("user.dir");

        String userdirName = new File(userdir).getName();
        if (userdirName.equalsIgnoreCase("lib")
                || userdirName.equalsIgnoreCase("bin")) {
            File newf = new File(userdir);
            File newp = new File(newf.getParent());
            if (fileName.trim().equals("")) {
                path = newp.getPath() + File.separator + parentDir;
            } else {
                path = newp.getPath() + File.separator + parentDir
                        + File.separator + fileName;
            }
        } else {
            if (fileName.trim().equals("")) {
                path = userdir + File.separator + parentDir;
            } else {

                path = userdir + File.separator + parentDir + File.separator+ fileName;
            }
        }
        return path;
    }*/
}
